package com.central.apps.service.impl;

import com.central.app.common.cache.CfgRedisCache;
import com.central.app.dao.CommonDao;
import com.central.app.model.BizAttribute;
import com.central.app.model.BizObject;
import com.central.apps.service.TableService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 数据表接口实现类
 *
 * @author 李大超
 * @date 2023/3/30
 */
@Slf4j
@Service
public class TableServiceImpl implements TableService {

    @Autowired
    private CfgRedisCache cfgRedisCache;

    @Override
    public Map<String, Object> convertMap(String fromTableName, String toTableName, Map<String, Object> data,
                                          Map<String, List<String>> convertMap, List<String> expFields) throws Exception {
        BizObject fromBizObject = cfgRedisCache.getCacheObject(fromTableName);
        BizObject toBizObject = cfgRedisCache.getCacheObject(toTableName);

        List<BizAttribute> fromBizAttributes = fromBizObject.getAttributes();
        List<BizAttribute> toBizAttributes = toBizObject.getAttributes();

        // 组织相同列数据
        if (CollectionUtils.isNotEmpty(fromBizAttributes) && CollectionUtils.isNotEmpty(toBizAttributes)) {
            if (convertMap == null) {
                convertMap = new HashMap<>();
            }

            if (expFields == null) {
                expFields = new ArrayList<>();
            }

            // 默认排除字段
            expFields.add("id");
            expFields.add("updatetime");
            expFields.add("updateuser");
            expFields.add("status");
            expFields.add("version");

            for (BizAttribute fromBizAttribute : fromBizAttributes) {
                String fromColumnName = fromBizAttribute.getColumnname();

                // 排除字段逻辑
                if (expFields.contains(fromColumnName)) {
                    continue;
                }

                for (BizAttribute toBizAttribute : toBizAttributes) {
                    String toColumnName = toBizAttribute.getColumnname();

                    if (fromColumnName.equals(toColumnName)) {
                        List<String> tempList = convertMap.get(fromColumnName);
                        if (CollectionUtils.isNotEmpty(tempList) && !tempList.contains(fromColumnName)) {
                            convertMap.get(fromColumnName).add(toColumnName);
                        } else {
                            convertMap.computeIfAbsent(fromColumnName, key -> new ArrayList<>()).add(toColumnName);
                        }
                        break;
                    }
                }
            }

            if (!convertMap.isEmpty()) {
                Map<String, Object> resMap = new HashMap<>();
                for (Map.Entry<String, Object> dataEntry : data.entrySet()) {
                    for (Map.Entry<String, List<String>> convertEntry : convertMap.entrySet()) {
                        if (dataEntry.getKey().equals(convertEntry.getKey())) {
                            List<String> tempList = convertEntry.getValue();
                            for (String tempStr : tempList) {
                                resMap.put(tempStr, dataEntry.getValue());
                            }
                            break;
                        }
                    }
                }

                return resMap;
            }
        }

        return null;
    }

    @Override
    public Map<String, Object> convertMap(String fromTableName, String toTableName, Map<String, Object> data) throws Exception {
        BizObject fromBizObject = cfgRedisCache.getCacheObject(fromTableName);
        BizObject toBizObject = cfgRedisCache.getCacheObject(toTableName);

        List<BizAttribute> fromBizAttributes = fromBizObject.getAttributes();
        List<BizAttribute> toBizAttributes = toBizObject.getAttributes();

        // 组织相同列数据
        if (CollectionUtils.isNotEmpty(fromBizAttributes) && CollectionUtils.isNotEmpty(toBizAttributes)) {
            Map<String, String> convertMap = new HashMap<>();
            for (BizAttribute fromBizAttribute : fromBizAttributes) {
                String fromColumnName = fromBizAttribute.getColumnname();

                // 只处理新增字段
                if (fromColumnName.startsWith("new")) {
                    for (BizAttribute toBizAttribute : toBizAttributes) {
                        String toColumnName = toBizAttribute.getColumnname();

                        if (fromColumnName.replace("new", "").equals(toColumnName)) {
                            convertMap.put(fromColumnName, toColumnName);
                            break;
                        }
                    }
                }
            }

            if (!convertMap.isEmpty()) {
                Map<String, Object> resMap = new HashMap<>();
                for (Map.Entry<String, Object> dataEntry : data.entrySet()) {
                    for (Map.Entry<String, String> convertEntry : convertMap.entrySet()) {
                        if (dataEntry.getKey().equals(convertEntry.getKey())) {
                            resMap.put(convertEntry.getValue(), dataEntry.getValue());
                            break;
                        }
                    }
                }

                return resMap;
            }
        }
        return null;
    }
}
